//
//  AppDelegate.swift
//  weibo_Swift
//
//  Created by 涂真 on 15/12/15.
//  Copyright © 2015年 涂真. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAppearance()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //        //测试性特性
        
        //        window?.rootViewController = TZNewFeatureController()
        
        //        //        测试欢迎登录
        //        NSThread.sleepForTimeInterval(2)
        //        window?.rootViewController = TZWelcomeController()
        //        //MARK:自定义tabBarController
//        let rootController = TZMainController()
//        window?.rootViewController = rootController
        //判断是否为最新版本
        if !isNewVersion(){
            //不是最新版本
            window?.rootViewController = TZNewFeatureController()
        }else{
            //是最新版本
            //判断是进入欢迎界面还是访客界面
            window?.rootViewController = defultViewController()
        }
        window?.makeKeyAndVisible()
        
        return true
    }
    ///设置主页导航栏按钮颜色
    func setupAppearance(){
        let appearance = UINavigationBar.appearance()
        appearance.tintColor = UIColor.orangeColor()
    }
    
    //MARK: - 对切换界面的条件做一些判断
    ///判断版本是否为最新版本
    func isNewVersion()->Bool{
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let sandboxVersionKey = "sandboxVersionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().objectForKey(sandboxVersionKey) as? String
        if currentVersion != sandBoxVersion{
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxVersionKey)
            return false
        }
        return true
    }
    
    ///判断新特性过后或是最新版本启动的默认控制器
    func defultViewController()->UIViewController{
        if TZUserAccount.isLogin{
            return TZWelcomeController()
        }
        return TZMainController()
        
    }
    
    ///加载目标控制器
    func switchRootController(isMain:Bool){
        window?.rootViewController = isMain ? TZMainController():TZWelcomeController()
    }
    
}

